package com.jiang.jetpackdemo.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.fragment.NewsRecommendFragment
import kotlinx.android.synthetic.main.activity_news_content.*


class NewsContentActivity : AppCompatActivity() {
companion object{
   const val USER_AGENT ="Mozilla/5.0 (Linux; Android 4.2.1; M040 Build/JOP40D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36"

}
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_news_content)
        getData()
    }


    private fun getData() {
        val stringExtra = intent.getStringExtra(NewsRecommendFragment.INTENT_KEY)
        if (stringExtra.isNullOrEmpty()) {
            Toast.makeText(applicationContext, "error", Toast.LENGTH_SHORT).show()
            finish()
        }
        wv.settings.useWideViewPort = true
        wv.settings.loadWithOverviewMode = true
        wv.settings.userAgentString = USER_AGENT
        wv.settings.javaScriptEnabled = true
        wv.loadUrl(stringExtra)
        wv.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                view?.loadUrl(request?.url.toString())
                return true
            }
        }


    }

//    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
//            wv.goBack()
//            return true
//        }
//        return super.onKeyDown(keyCode, event)
//    }

    override fun onDestroy() {
        super.onDestroy()
        wv.clearHistory()
        wv.destroy()
    }
}
